package com.hmkj.quartz.job;

import com.alibaba.fastjson.JSON;
import com.hmkj.core.constant.CacheID;
import com.hmkj.core.eolinker.endpoints.QuotationEndpoint;
import com.hmkj.core.eolinker.models.StockListModel;
import com.hmkj.core.eolinker.models.StockType;
import com.hmkj.core.service.cache.RedisService;
import org.apache.log4j.Logger;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author
 * @date 2017/8/17
 */
@Component
@Lazy(value = false)
public class QuoteListJob {
    @Resource
    RedisService redisService;

    private final Logger L = Logger.getLogger(QuoteListJob.class);

    /**
     * 查询所有股票信息
     */
    @Scheduled(cron = "0/1 * * * * ?")
//    @Scheduled(cron = "0 17,21 9 * * MON-FRI")
    //在每天上午9点到上午9:10期间的每1分钟触发
    public void findAllStock() throws Exception {
        L.info("stockList");
        //上证A股， 深证A股
        String jsonStrUp = QuotationEndpoint.list(StockType.UP_A, "2000", "0");
        String jsonStrShen = QuotationEndpoint.list(StockType.SHEN_A, "2000", "0");
        String jsonStrUpIndex = QuotationEndpoint.list(StockType.UP_Index, "2000", "0");
        String jsonStrShenIndex = QuotationEndpoint.list(StockType.SHEN_Index, "2000", "0");
        String jsonStrMS = QuotationEndpoint.list(StockType.M_S, "2000", "0");
        String jsonStrSTARTUPS = QuotationEndpoint.list(StockType.STARTUPS, "2000", "0");
        List<StockListModel> listModels = new ArrayList<>();
        //上证A股信息列表
        List<StockListModel> listUp = JSON.parseArray(jsonStrUp, StockListModel.class);
        if (listUp != null && listUp.size() > 1400) {
            listModels.addAll(listUp);
        } else {
            findAllStock();
        }
        //深证A股信息列表
        List<StockListModel> listShen = JSON.parseArray(jsonStrShen, StockListModel.class);
        if (listShen != null && listShen.size() > 450) {
            listModels.addAll(listShen);
        } else {
            findAllStock();
        }
        //深证指数信息列表
        List<StockListModel> listShenIndex = JSON.parseArray(jsonStrShenIndex, StockListModel.class);
        if (listShenIndex != null && listShenIndex.size() > 0) {
            listModels.addAll(listShenIndex);
        } else {
            findAllStock();
        }
        //上证指数信息列表
        List<StockListModel> listUpIndex = JSON.parseArray(jsonStrUpIndex, StockListModel.class);
        if (listUpIndex != null && listUpIndex.size() > 0) {
            listModels.addAll(listUpIndex);
        } else {
            findAllStock();
        }
        //中小板信息列表
        List<StockListModel> listMs = JSON.parseArray(jsonStrMS, StockListModel.class);
        if (listMs != null && listMs.size() > 900) {
            listModels.addAll(listMs);
        } else {
            findAllStock();
        }
        //创业板信息列表
        List<StockListModel> listStarUp = JSON.parseArray(jsonStrSTARTUPS, StockListModel.class);
        if (listStarUp != null && listStarUp.size() > 700) {
            listModels.addAll(listStarUp);
        } else {
            findAllStock();
        }
        if (listModels.size() > 0) {
            redisService.put(CacheID.STOCK_LIST_PREFIX, listModels);
        } else {
            findAllStock();
        }

    }
}
